package com.treasure.hunt.task

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.module.lemlin.base.loadCenterImage
import com.module.lemlin.base.loadCropImage
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.mode.GridViewMode
import kotlinx.android.synthetic.main.fragment_grid_prize_pool.*

class GridPrizePoolFragment : ViewModeFragment<GridViewMode>() {

    companion object {
        fun newInstance(): GridPrizePoolFragment {
            return GridPrizePoolFragment()
        }
    }

    override fun viewModel(): GridViewMode {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_grid_prize_pool

    override fun onResume() {
        super.onResume()
        viewModel.otherGetImage("daily_image")
    }

    override fun initView() {
        viewModel.otherImageLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (response.status != Status.SUCCESS)
                return@Observer
            response.httpData?.data?.image?.let { image ->
                val isHttps: Boolean = image.substring(0, 5).contains("http") ||
                        image.substring(0, 5).contains("https")
                ivGridPrizePoolImage.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${image}")
            }
        })
    }
}